// app.js
const config = require('./config/api')

App({
  onLaunch() {
    if (!wx.cloud) {
      console.error('请使用 2.2.3 或以上的基础库以使用云能力');
    } else {
      wx.cloud.init({
        env: 'your-env-id', // 替换为你的云开发环境ID
        traceUser: true,
      }).catch(e => {
        console.error('云开发初始化失败：', e);
      });
    }

    // 获取系统信息
    wx.getSystemInfo({
      success: e => {
        this.globalData.StatusBar = e.statusBarHeight;
        let custom = wx.getMenuButtonBoundingClientRect();
        this.globalData.Custom = custom;
        this.globalData.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
      }
    });

    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })

    this.globalData = {
      userInfo: null,
      isCoach: false,  // 用户是否为教练
      currentActivity: null,  // 当前选中的活动
      config: config,
      StatusBar: null,
      Custom: null,
      CustomBar: null
    }
  },
  globalData: {
    userInfo: null,
    StatusBar: null,
    Custom: null,
    CustomBar: null,
    config: {
      env: 'dev',
      baseUrl: {
        dev: 'http://localhost:8080',
        prod: 'https://your-production-url'
      },
      apiPath: {
        upload: {
          image: '/api/upload/image'
        }
      }
    }
  }
})
